package com.koron.car.web.event;

import com.koron.car.web.motorcade.bean.Motorcade;
import com.koron.car.web.motorcade.service.MotorcadeService;
import com.koron.common.core.config.ThreadLocalContext;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author twcao
 * @title: EventListener
 * @projectName GDH_EAM_V2
 * @description: 车辆事件监听
 * @date 2022/2/2616:10
 */
@Slf4j
@Component
public class MotorcadeEventListener {

    @Autowired
    private MotorcadeService motorcadeService;

    @Async
    @EventListener
    public void onMotorcadeEvent(MotorcadeEvent event) {
        log.info("name = {}", event.getMotorcade());
        try (SessionFactory factory = new SessionFactory()) {
            factory.setDbEnv(ThreadLocalContext.get().getCurrDs());
            motorcadeService.saveOrUpdate(factory, new Motorcade().setName(event.getMotorcade()));
        }
    }
}
